package cisdi.mes.sm.rm.serviceImpl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cisdi.mes.sm.rm.dao.MeasureDocDao;
import cisdi.mes.sm.rm.dao.MeasureDocLogDao;
import cisdi.mes.sm.rm.dao.MatRequestDao;
import cisdi.mes.sm.rm.domain.SmRmMaterialRequest;
import cisdi.mes.sm.rm.domain.SmRmMeasureDoc;
import cisdi.mes.sm.rm.service.MatReceiveMgr;
import cisdi.mes.sm.util.dao.BaseDao;
import cisdi.mes.sm.util.dao.IRvDao;
import cisdi.mes.sm.util.daoImpl.IdMapping;

@Service
public class MatReceiveMgrImpl implements MatReceiveMgr {

	@Autowired
	private BaseDao baseDao;
	
	@Autowired
	private IdMapping idMapping;
	
	@Autowired
	private MeasureDocDao measureDocDao;
	
	@Autowired
	private MeasureDocLogDao measureDocLogDao;
	
	@Autowired
	protected IRvDao rvDao;
	
	@Autowired
	private MatRequestDao matRequestDao;

	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public IdMapping getIdMapping() {
		return idMapping;
	}

	public void setIdMapping(IdMapping idMapping) {
		this.idMapping = idMapping;
	}

	public MeasureDocDao getMeasureDocDao() {
		return measureDocDao;
	}

	public void setMeasureDocDao(MeasureDocDao measureDocDao) {
		this.measureDocDao = measureDocDao;
	}

	public MeasureDocLogDao getMeasureDocLogDao() {
		return measureDocLogDao;
	}

	public void setMeasureDocLogDao(MeasureDocLogDao measureDocLogDao) {
		this.measureDocLogDao = measureDocLogDao;
	}

	public IRvDao getRvDao() {
		return rvDao;
	}

	public void setRvDao(IRvDao rvDao) {
		this.rvDao = rvDao;
	}

	public MatRequestDao getMatReqDao() {
		return matRequestDao;
	}

	public void setMatRequestDao(MatRequestDao matRequestDao) {
		this.matRequestDao = matRequestDao;
	}

	@Transactional
	@Override
	public Map<String, Object> inputReceive(SmRmMeasureDoc userDoc,
			HttpServletRequest request) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		String measureId = idMapping.GetFinalId("measure_id", null);
		String qualityBatch = idMapping.GetFinalId("quality_batch", null);
		userDoc.setMeasureId(measureId);
		userDoc.setQualityBatch(qualityBatch);
	
		// 计量单收货
		measureDocDao.inputRcv(userDoc);
		SmRmMeasureDoc measureDoc = measureDocDao.getById(measureId);
		measureDocLogDao.logMatRcv(measureDoc, request);

		map.put("success", true);
		map.put("message", "手工录入收货成功");
		return map;
	}

	@Transactional
	@Override
	public Map<String, Object> matReceive(SmRmMeasureDoc userDoc,
			HttpServletRequest request) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		String measureId = userDoc.getMeasureId();

		String validString = validMatReceive(measureId);
		if (validString.equals("success")) {
			String deliveryType = userDoc.getDeliveryType();
			// 计量单收货
			String qualityBatch = idMapping.GetFinalId("quality_batch", null);// 创建成分质检批次
			userDoc.setQualityBatch(qualityBatch);
			if (deliveryType.endsWith("配送")) {
				SmRmMaterialRequest matReq = matRequestDao.getByReqId(userDoc
						.getPurchaseOrder());
				matReq.setIsReceived("Y");
				matReq.setLogisticsStatus("RECEIVE");
				baseDao.update(matReq);
			}
			measureDocDao.matRcv(userDoc);
			SmRmMeasureDoc measureDoc = measureDocDao.getById(measureId);
			measureDocLogDao.logMatRcv(measureDoc, request);
			rvDao.sendMatRcvForM(measureDoc);

			map.put("success", true);
			map.put("message", "计量单收货成功");
		} else {
			map.put("success", false);
			map.put("message", validString);
		}

		return map;
	}

	private String validMatReceive(String measureId) {
		String validString = "";
		Boolean isNotRcv = false;
		SmRmMeasureDoc md = measureDocDao.getById(measureId);
		if (md != null && !md.getStatus().equals("RECEIVE")
				&& md.getMaterialDesc() != null) {
			isNotRcv = true;
		} else {
			if (md.getMaterialDesc() == null) {

				validString += "*该计量单的物料描述为空,物料编码在物料主数据中找不到";
			} else {
				validString += "*该计量单的进厂状态为" + md.getStatus() + "<br>";
			}
		}

		if (isNotRcv) {
			validString = "success";
		}

		return validString;
	}

}
